function ScanService() { }

ScanService.prototype = {
    scanStart: function(){
        try {
            window.plugins.barcodeScanner.scan(
                // 成功時に実行されるコールバック（キャンセル時も含む）
                function(result) {
                    // キャンセルされたら何もしない
                    if (result.cancelled) {
                        return;
                    }
                    var barcode = result.text;
                    if ($.isNumeric(barcode)) {
                        if (Util.prototype.showInternetAlert()) {
                            Util.prototype.showSpinner();
                            DetailProductController.prototype.params = {"barcode_number" : barcode, "doScanAction" : true};        
                            if ($.mobile.activePage.attr("id") == "pageItemList") {
                                DetailProductController.prototype.loadData();
                            } else {
                                PageChangeController.prototype.changeToPage("pageItemList");
                            }
                        }
                    } else {
                        alert("商品コードではありません。");
                    }
                },
                // エラー時に実行されるコールバック
                function(error) {
                    alert("Scanning failed: " + error);
                }
            );
        } catch(ex){
            console.log(ex.message);
        }        
    },
    
    getScanAddress : function(callback, callbackError) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var curPosData = {};
            curPosData.latitude = position.coords.latitude;
            curPosData.longitude = position.coords.longitude;
            $.ajax({
                type: 'GET',
                url: 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&latlng=' + curPosData.latitude + ',' + curPosData.longitude + '&sensor=true&region=jp',
                dataType: 'json',
                timeout: 1000 * 20,
                success: function(positionData){
                    if (positionData.status == "OK") {
                        console.log("1");
                        var addressData = positionData.results[0].address_components;
                        var country_name = "", 
                        pref_name = "", 
                        city_name = "";
                        if(addressData.length > 2){
                            for(var i = addressData.length-1; i>=0 ; i-- ) {
                                var addType = addressData[i].types[0];
                                if(addType == "country") {
                                    country_name = addressData[i].long_name;
                                } else if(addType == "administrative_area_level_1") {
                                    pref_name = addressData[i].long_name;
                                } else if(addType == "locality") {
                                    city_name = addressData[i].long_name;
                                    break;
                                }
                            }
                            curPosData.address = pref_name + " " + city_name;
                            
                            PrefecturesService.prototype.searchPrecturesCode(country_name, pref_name, city_name, function(results) {
                                if (results.rows.length > 0) {
                                    curPosData.prefecture_code = results.rows.item(0).code;
                                    curPosData.pref_no = results.rows.item(0).pref_no;
                                }
                                callback(curPosData);
                            });
                        } else {
                            callback(curPosData);
                        }
                    } else {
                        callback(curPosData);
                    }
                }, error: callbackError 
            });
        }, callbackError, {timeout: 1000 * 20});
    },
   
};